/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EconosimUI.java
 *
 * Created on Apr 27, 2009, 2:29:02 PM
 */

package viewer.common;


import javax.swing.*;

import simframe.simulation.SimViewer;
import simframe.simulation.Time;
import simframe.simulation.Simulation;

import viewer.parameters.ParmView;

/**
 *
 * @author karakots
 */
public class SimView extends javax.swing.JFrame {


      // this is days per second
    static final int minDays = 1;
    static final int maxDays = 60;

    /** Creates new form EonoSimUI */
    public SimView(Simulation simIn, JPanel pane) {
        this.sim = simIn;

    	initComponents();
        //SpinnerNumberModel model = new SpinnerNumberModel(maxDays,1,maxDays+1,1);
      
        //
        // Map View
        //
        mapPanel.add(pane);

        SimViewer dayView = new SimViewer() {

            public void update(long t) {

                status.setText("Day " + Long.toString(t/Time.Day));
            }

            public void reset() {
               status.setText("reset");
            }

        };

         status.setText("Initialzing Econosim");
        
         sim.addViewer(dayView, 5);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        mapGroup = new javax.swing.ButtonGroup();
        toolBar = new javax.swing.JToolBar();
        startButton = new javax.swing.JButton();
        resetButton = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        mapPanel = new javax.swing.JPanel();
        statusPanel = new javax.swing.JPanel();
        status = new javax.swing.JTextField();

        org.jdesktop.layout.GroupLayout jFrame1Layout = new org.jdesktop.layout.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                simViewWindowClosing(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentHidden(java.awt.event.ComponentEvent evt) {
                simViewHidden(evt);
            }
        });

        toolBar.setBorder(null);
        toolBar.setRollover(true);
        toolBar.setAlignmentY(1.0F);
        toolBar.setMaximumSize(new java.awt.Dimension(500, 27));
        toolBar.setMinimumSize(new java.awt.Dimension(500, 27));
        toolBar.setPreferredSize(new java.awt.Dimension(500, 25));
        toolBar.setVerifyInputWhenFocusTarget(false);

        startButton.setText("Start");
        startButton.setFocusable(false);
        startButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        startButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        startButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startButtonActionPerformed(evt);
            }
        });
        toolBar.add(startButton);

        resetButton.setText("Reset");
        resetButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetButtonActionPerformed(evt);
            }
        });
        toolBar.add(resetButton);

        jButton1.setText("Parameters");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        toolBar.add(jButton1);

        jSeparator1.setSeparatorSize(new java.awt.Dimension(10, 0));
        toolBar.add(jSeparator1);

        jSeparator2.setSeparatorSize(new java.awt.Dimension(10, 0));
        toolBar.add(jSeparator2);

        getContentPane().add(toolBar, java.awt.BorderLayout.NORTH);

        mapPanel.setPreferredSize(new java.awt.Dimension(300, 325));
        mapPanel.setLayout(new java.awt.BorderLayout());
        getContentPane().add(mapPanel, java.awt.BorderLayout.CENTER);

        statusPanel.setMaximumSize(new java.awt.Dimension(2147483647, 20));
        statusPanel.setPreferredSize(new java.awt.Dimension(300, 20));
        statusPanel.setLayout(new java.awt.BorderLayout());

        status.setEditable(false);
        status.setText("This is the time for all good men to come to the aid of the party");
        statusPanel.add(status, java.awt.BorderLayout.PAGE_END);

        getContentPane().add(statusPanel, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void resetButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetButtonActionPerformed
        // TODO add your handling code here:
        startButton.setText("Start");
        sim.reset();
    }//GEN-LAST:event_resetButtonActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

        if (parmView == null) {
          
            try {
                parmView = new ParmView(sim.getParameters());
            } catch (Exception e) {
            }
              
            parmView.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        }

        parmView.setVisible(true);
 
    }//GEN-LAST:event_jButton1ActionPerformed

    private void startButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startButtonActionPerformed
          if (sim.stopped()) {
            if (startButton.getText().compareTo("Start") == 0) {
                sim.reset();
            }
            // setDaysToRun();
            simThread = new Thread(sim);
            simThread.start();
            startButton.setText("Pause");
            resetButton.setEnabled(false);
        } else {
        	sim.stop();
        	simThread = null;
        	startButton.setText("Continue");
            resetButton.setEnabled(true);
        }
    }//GEN-LAST:event_startButtonActionPerformed

    private void simViewWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_simViewWindowClosing

        sim.stop();

        dispose();
    }//GEN-LAST:event_simViewWindowClosing

    private void simViewHidden(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_simViewHidden
       sim.stop();
    }//GEN-LAST:event_simViewHidden


     JFrame parmView = null;
    private final Simulation sim;

    private static final long serialVersionUID = 1L;

  

    private Thread simThread;
    

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.ButtonGroup mapGroup;
    private javax.swing.JPanel mapPanel;
    private javax.swing.JButton resetButton;
    private javax.swing.JButton startButton;
    private javax.swing.JTextField status;
    private javax.swing.JPanel statusPanel;
    private javax.swing.JToolBar toolBar;
    // End of variables declaration//GEN-END:variables

}
